package com.bestalbum.editor{
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest; 
	 		import flash.utils.ByteArray;
		import flash.display.Shader;
		import flash.display.ShaderData;
		import flash.filters.ShaderFilter; 
		
		/**
		 * 
 		* 		
 		import flash.utils.ByteArray;
		import flash.display.Shader;
		import flash.display.ShaderData;
		import flash.filters.ShaderFilter; 
		
 		**/
	
	public class PresetsFactory{
		
		private static var INSTANCE:PresetsFactory = new PresetsFactory();
		private var filters:Array = [];
		private var loader:URLLoader;
		private var loadingFilter:Number = -1;

		public function PresetsFactory(){}
		
		public static function getInstance():PresetsFactory {
      		return INSTANCE;
   		}
   		
   		public function prepareFilters(_xmlFilters:XMLList):void{
   			trace("PresetsFactory::prepareFilters() _xmlFilters="+_xmlFilters); 
			for (var i:int = 0; i < _xmlFilters.length(); i++){ 
				var filter:Object = new Object();
				filter.id 	= _xmlFilters[i].@id;
				filter.name = _xmlFilters[i].@name;
				filter.file = _xmlFilters[i].@file;
				var params:XMLList = _xmlFilters[i].filterParameters.parameter;
				var pars:Array = new Array();
				for (var j:int = 0; j < params.length(); j++){ 
					var parameter:Object = new Object();
					parameter.name = params[j].@name;
					parameter.value = params[j].@value;
					parameter.type = params[j].@type;
					parameter.min_value = params[j].@min_value;
					parameter.max_value = params[j].@max_value;
					parameter.def_value = params[j].@def_value;
					pars.push(parameter);
				}
				filter.pars = pars;
				filter.pbj_loaded = false;
				filters.push(filter);
			}
			loadPBJ();
   		}
   		
   		private function loadPBJ():void{
   			trace("PresetsFactory::loadPBJ() filters.length="+filters.length); 
   			for (var i:int = 0; i < filters.length; i++){ 
   				trace("PresetsFactory::loadPBJ() filters["+i+"].pbj_loaded="+filters[i].pbj_loaded +" :: " + filters[i].file); 
   				if(!filters[i].pbj_loaded) {
   					loadingFilter = i;
   					loader = new URLLoader(); 
		            loader.dataFormat = URLLoaderDataFormat.BINARY; 
		            loader.addEventListener(Event.COMPLETE, onLoadComplete); 
		            loader.load(new URLRequest(Preset.PRESETS_FOLDER + "/" + filters[i].file)); 
		            break;
   				}
   			}
   		}
   		
   		private function onLoadComplete(event:Event):void {
			trace("PresetsFactory::onLoadComplete()"); 
			trace("PresetsFactory::onLoadComplete() filters[loadingFilter].pars.length="+filters[loadingFilter].pars.length); 
		    var shader:Shader = new Shader(loader.data); 
		    if(filters[loadingFilter].pars.length > 0){
		    	var sd:ShaderData = shader.data;
		    	sd.intensity = 0.2;
		    	shader.data = sd;
		    }
		    filters[loadingFilter].pbj = shader;
		    filters[loadingFilter].pbj_loaded = true;
		    loadPBJ();
		}
		
		public function applyPreset(imageTarget:MovieClip, selectedFilters:XMLList){
			for (var i:int = 0; i < selectedFilters.length(); i++){ 
				var idFilter:Number = Number(selectedFilters[i].@id);
				var filterObject = findFilterObject(idFilter);
				trace("PresetsFactory::applyPreset() idFilter="+idFilter+" "+filterObject.name+" pars: "+filterObject.pars.length);
				var filter:ShaderFilter = new ShaderFilter(filterObject.pbj);
				imageTarget.filters = [filter]; 
			}  
		}
		
		private function findFilterObject(id:Number):Object{
			for (var i:int = 0; i < filters.length; i++) if(Number(filters[i].id) == id) return filters[i];
			return null;
		}
	}
}